package com.bf.electroplating.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bf.electroplating.login.UserContext;
import com.bf.electroplating.login.UserDetail;
import com.bf.electroplating.mapper.ComContingencySpaceFacilityMapper;
import com.bf.electroplating.pojo.dto.contingency.ContingencySpaceFacilityDTO;
import com.bf.electroplating.pojo.dto.contingency.ContingencySpaceFacilityQueryDTO;
import com.bf.electroplating.pojo.entity.ComCompany;
import com.bf.electroplating.pojo.entity.ComContingencySpaceFacility;
import com.bf.electroplating.pojo.enums.SpaceFacilityTypeEnum;
import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.pojo.vo.contingency.ContingencySpaceFacilityVO;
import com.bf.electroplating.service.ComCompanyService;
import com.bf.electroplating.service.ComContingencySpaceFacilityService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author 15451
* @description 针对表【com_contingency_space_facility(应急空间设施表)】的数据库操作Service实现
* @createDate 2025-05-09 11:11:32
*/
@Service
public class ComContingencySpaceFacilityServiceImpl extends ServiceImpl<ComContingencySpaceFacilityMapper, ComContingencySpaceFacility>
    implements ComContingencySpaceFacilityService{
	@Resource
	private ComCompanyService comCompanyService;

	@Override
	public ResultVO<Page<ContingencySpaceFacilityVO>> pageList(ContingencySpaceFacilityQueryDTO queryDTO) {
		Page<ComContingencySpaceFacility> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
		Map<String, String> companyMap = comCompanyService.list().stream().collect(Collectors.toMap(ComCompany::getId, ComCompany::getCompanyName));
		LambdaQueryWrapper<ComContingencySpaceFacility> wrapper = new LambdaQueryWrapper<>();

		if (StringUtils.isNotBlank(queryDTO.getCompanyId())) {
			wrapper.eq(ComContingencySpaceFacility::getCompanyId, queryDTO.getCompanyId());
		}
		if (StringUtils.isNotBlank(queryDTO.getType())) {
			wrapper.eq(ComContingencySpaceFacility::getType, queryDTO.getType());
		}
		UserDetail user = UserContext.getUser();

		wrapper.orderByDesc(ComContingencySpaceFacility::getCreateTime);

		Page<ComContingencySpaceFacility> resultPage = this.page(page, wrapper);

		// 转换为VO
		Page<ContingencySpaceFacilityVO> voPage = new Page<>();
		BeanUtils.copyProperties(resultPage, voPage, "records");

		List<ContingencySpaceFacilityVO> voList = new ArrayList<>();
		for (ComContingencySpaceFacility entity : resultPage.getRecords()) {
			ContingencySpaceFacilityVO vo = new ContingencySpaceFacilityVO();
			BeanUtils.copyProperties(entity, vo);
			vo.setType(SpaceFacilityTypeEnum.getDesc(entity.getType()));
			vo.setCompanyName(companyMap.get(entity.getCompanyId()));
			voList.add(vo);
		}
		voPage.setRecords(voList);

		return ResultVO.ok(voPage);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResultVO<String> saveData(ContingencySpaceFacilityDTO dto) {
		ComContingencySpaceFacility facility = new ComContingencySpaceFacility();
		BeanUtils.copyProperties(dto, facility);
		saveOrUpdate(facility);

		return ResultVO.ok(null, "保存成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public ResultVO<String> delete(List<String> ids) {
		if (ids == null || ids.isEmpty()) {
			return ResultVO.fail("ID不能为空");
		}

		this.removeByIds(ids);
		return ResultVO.ok(null, "删除成功");
	}

	@Override
	public ResultVO<ContingencySpaceFacilityVO> getDetail(String id) {
		if (StringUtils.isBlank(id)) {
			return ResultVO.fail("ID不能为空");
		}
		ComContingencySpaceFacility entity = this.getById(id);
		if (entity == null) {
			return ResultVO.fail("记录不存在");
		}
		Map<String, String> companyMap = comCompanyService.list().stream().collect(Collectors.toMap(ComCompany::getId, ComCompany::getCompanyName));
		ContingencySpaceFacilityVO vo = new ContingencySpaceFacilityVO();
		BeanUtils.copyProperties(entity, vo);
		vo.setCompanyName(companyMap.get(entity.getCompanyId()));

		return ResultVO.ok(vo);
	}
}




